package com.dy.cloud.controller;

import com.dy.cloud.pojo.Entity.PaymentEntity;
import com.dy.cloud.util.ApiResousUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ClassName: OrderController
 * @Author: 仄言in
 * @Desctiption: (描述) 调用 cloud-provider-payment-8001服务
 * @Date: 2021/9/3 14:21
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    //private static final String REST_ARRDS="http://localhost:8001/payment";
    private static final String REST_ARRDS="http://CLOUDPROVIDERPAYMENT/payment";

    /**
     * 查询
     * @return ApiResousUtil<List>
     **/
    @GetMapping("/list")
    public ApiResousUtil<List> list(){
        log.info("---list---");
        return ApiResousUtil.ok(restTemplate.getForObject(REST_ARRDS+"/list", ApiResousUtil.class));
    }
    /**
     * 添加
     * @param payment
     * @return
     **/
    @PostMapping("/create")
    public ApiResousUtil<String> create(@RequestBody PaymentEntity payment){
        log.info("create");
        return ApiResousUtil.ok(restTemplate.postForObject(REST_ARRDS+"/save",payment,ApiResousUtil.class));
    }
    /**
     * 删除
     * @param id
     * @return
     **/
    @GetMapping("/deleteById/{id}")
    public ApiResousUtil<String> deleteById(@PathVariable("id") Long id){
        log.info("--deleteById--");
        restTemplate.getForEntity(REST_ARRDS+"/deleteById/"+id, ApiResousUtil.class);
        return ApiResousUtil.ok();
    }
    /**
     * 根据id查询
     * @param id
     * @return
     **/
    @GetMapping("/selectById/{id}")
    public ApiResousUtil<String> selectById(@PathVariable("id") Long id){
        return ApiResousUtil.ok(restTemplate.getForEntity(REST_ARRDS+"/selectById/"+id,ApiResousUtil.class).getBody());

    }
    /**
     * 修改
     * @param payment
     * @return
     **/
    @PostMapping("/updateById")
    public ApiResousUtil<String> updateById(@RequestBody PaymentEntity payment){
        restTemplate.postForObject(REST_ARRDS+"/updateById",payment,ApiResousUtil.class);
        return ApiResousUtil.ok();
    }

}
